// Mask.cpp : implementation file
//

#include "stdafx.h"
#include "Mask.h"


// CMask

CMask::CMask(MaskType type) : m_type(type)
{
}

CMask::~CMask()
{
}


// CMask member functions

BOOL CMask::Apply(CxImage& srcImage, CxImage& mask)
{
	if (srcImage.ResetIteration() && mask.ResetIteration())
	{
		RGBQUAD value1, value2;
		if (m_type == INTERSECTION)
		{
			while (srcImage.GetNextPixelColor(value1) && 
				mask.GetNextPixelColor(value2))
			{
				value1.rgbRed = min(value1.rgbRed, value2.rgbRed);
				value1.rgbGreen = min(value1.rgbGreen, value2.rgbGreen);
				value1.rgbBlue = min(value1.rgbBlue, value2.rgbBlue);

				srcImage.SetCurrentPixel(value1);
			}
			return TRUE;
		}
		else if (m_type == UNION)
		{
			while (srcImage.GetNextPixelColor(value1) && 
				mask.GetNextPixelColor(value2))
			{
				value1.rgbRed = max(value1.rgbRed, value2.rgbRed);
				value1.rgbGreen = max(value1.rgbGreen, value2.rgbGreen);
				value1.rgbBlue = max(value1.rgbBlue, value2.rgbBlue);

				srcImage.SetCurrentPixel(value1);
			}
			return TRUE;
		}
	}
	return FALSE;
}